<?php
class TEquipement extends SQLite3
{

	public function __construct()	{
        	if(file_exists(PATH_BDD.'.db')) {
			$this->open(PATH_BDD.'.db');
		}
    	}

	public static function getListeEquipements($byRoom = false) {

		$db = new TEquipement();
		if($byRoom)$a_result = $db->query("SELECT * From EQUIPEMENT AS E JOIN PIECE AS P ON E.ID_PIECE=P.ID_PIECE GROUP BY ID_PIECE");
		else $a_result = $db->query("SELECT * From EQUIPEMENT");
		$_SESSION['equipements'] = array();

		$a_equipements = array();
		if(!empty($a_result)) {
			while($res = $a_result->fetchArray(SQLITE3_ASSOC)) {
				$a_equipements[] = $res;
			}
		}
		if(!empty($a_equipements) && is_array($a_equipements)) {
			if(!empty($_SESSION['equipements'])) {
				if($_SESSION['equipements'] !=  $a_equipements)
				{
					$_SESSION['equipements'] = $a_equipements;
					$json = array("status" => 0, "msg" => "Changement des equipements","equipements" => $a_equipements);
					echo $_GET['jsoncallback'] . '(' . json_encode($json) . ');';
				}
				else {
					$json = array("status" => 1, "msg" => "Aucun changement");
                                	echo $_GET['jsoncallback'] . '(' . json_encode($json) . ');';
				}
			}
			else {
				$_SESSION['equipements'] = $a_equipements;
				$json = array("status" => 2, "msg" => "Premiere demande","equipements" => $a_equipements);
				echo $_GET['jsoncallback'] . '(' . json_encode($json) . ');';
			}
		}
		else {
			$json = array("status" => 3, "msg" => "Aucun equipement");
                        echo $_GET['jsoncallback'] . '(' . json_encode($json) . ');';
		}

		$db->close();

		//echo $_GET['jsoncallback'] . '(' . json_encode($a_equipements) . ');';
	}

	public static function coupleEquipment() {
		$db = new TEquipement();
                $a_result = $db->query("SELECT Max(CODE_RADIO) AS CODE_RADIO From EQUIPEMENT");

                $i_code_radio;
                if(!empty($a_result)) {
                        $res = $a_result->fetchArray(SQLITE3_ASSOC);
                        $i_code_radio = $res['CODE_RADIO']+1 ;
                }
		else {
			$i_code_radio = 1;
		}
                for($i = 0;$i < 3;$i++) {
			system('/usr/bin/radioEmission 0 12345678 '.$i_code_radio.' on');
		}
		$json = array( "msg" => "Recuperation effectué","code_radio" => $i_code_radio);
                echo $_GET['jsoncallback'] . '(' . json_encode($json) . ');';

		$db->close();
	}



	public static function changerEtat($b_etat,$i_idEquipement) {

		$db = new TEquipement();
		$db->exec("UPDATE 'EQUIPEMENT' SET ETAT ='".$b_etat."' WHERE ID_EQUIPEMENT = ".$i_idEquipement);


		$a_result = $db->query("SELECT CODE_RADIO FROM EQUIPEMENT WHERE ID_EQUIPEMENT = ".$i_idEquipement);
		$res = $a_result->fetchArray(SQLITE3_ASSOC);
		$i_codeRadio = $res['CODE_RADIO'];


		system('/usr/bin/radioEmission 0 12345678 '.$i_codeRadio.' '.$b_etat);
		$return = $_POST;
		$return["json"] = json_encode($return);
		echo json_encode($return);

		$db->close();
	}
	public static function deleteEquipment($i_idEquipment) {
		if(!empty($i_idEquipment)) {
			$db = new TEquipement();
			$a_result = $db->query("SELECT NOM_EQUIPEMENT FROM EQUIPEMENT WHERE ID_EQUIPEMENT = ".$i_idEquipment);
                	$res = $a_result->fetchArray(SQLITE3_ASSOC);
                	$i_nameEquipement = $res['NOM_EQUIPEMENT'];


			$statement = $db->prepare('DELETE FROM EQUIPEMENT WHERE ID_EQUIPEMENT= :id;');
			$statement->bindValue(':id', $i_idEquipment);

			$statement->execute();
			$db->close();
			include_once("TEvenement.php");
			//TEvenement::addEvent();
			$return = array("idEquipement" => $i_idEquipment,"nameEquipement" => $i_nameEquipement);
                	$return["json"] = json_encode($return);
			echo json_encode($return);
		}
		else {
			echo json_encode(-1);
		}
	}

	public static function addEquipment($s_nameEquipment,$s_type,$i_codeRadio,$i_coordX,$i_coordY,$i_idRoom) {
		header('Content-type: application/json');
		$db = new TEquipement();


		$statement = $db->prepare('INSERT INTO EQUIPEMENT VALUES (NULL,:name,:type,:codeRadio,"off",:coordX,:coordY,"","",1)');
                $statement->bindValue(':name', $s_nameEquipment,SQLITE3_TEXT);
		$statement->bindValue(':type', $s_type,SQLITE3_TEXT);
		$statement->bindValue(':codeRadio', $i_codeRadio,SQLITE3_INTEGER);
		$statement->bindValue(':coordX', $i_coordX);
		$statement->bindValue(':coordY', $i_coordY);
		//$statement->bindValue(':room', $i_idRoom,SQLITE3_INTEGER);

		$result = $statement->execute();
                $db->close();

		$return["json"] = json_encode($result);
                echo json_encode($_POST);
	}
	public static function saveChangesEquipment($i_idEquipment,$s_name,$s_heureAllumage,$s_heureExtinction) {
		header('Content-type: application/json');
                $db = new TEquipement();
                $db->exec("UPDATE 'EQUIPEMENT' SET NOM_EQUIPEMENT ='".$s_name."',HEURE_ALLUMAGE = '".$s_heureAllumage."',HEURE_EXTINCTION = '".$s_heureExtinction."' WHERE ID_EQUIPEMENT = ".$i_idEquipment);
                $db->close();

                $return["json"] = json_encode($result);
                echo json_encode($_POST);
	}

}
?>
